<template>
  <div class="add-container">
    <div class="add-top">
      <el-page-header @back="goBack" :content="gobacktitle"></el-page-header>
    </div>
    <div class="add-main">
      <div class="add-main-top">
        <div class="add-main-tital">
          <p>{{ gobacktitle }}维护保养</p>
        </div>
        <div>
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            label-position="left"
            label-width="0.8rem"
            class="demo-ruleForm"
          >
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="状态" prop="zhuangtai">
                  <el-select v-model="ruleForm.zhuangtai" placeholder="请选择">
                    <el-option
                      v-for="(item, index) in zhuangtaiArr.data"
                      :key="index"
                      :label="item.mingcheng"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="物资区分" prop="wuziqf">
                  <el-select v-model="ruleForm.wuziqf" placeholder="请选择">
                    <el-option
                      v-for="(item, index) in $util.getWzqf()"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="库房" prop="kufangid">
                  <el-select v-model="ruleForm.kufangid" placeholder="请选择">
                    <el-option
                      v-for="(item, index) in KufangList"
                      :key="index"
                      :label="item.mingcheng"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="物资分类" prop="wuzizyid">
                  <el-select v-model="ruleForm.wuzizyid" placeholder="请选择">
                    <el-option
                      v-for="(item, index) in Wzzylist"
                      :key="index"
                      :label="item.zhuanyemc"
                      :value="item.zhuanyeid"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="主管单位" prop="zhuguandwid">
                  <el-select
                    v-model="ruleForm.zhuguandwid"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in Zgdwlist"
                      :key="index"
                      :label="item.danweimc"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="备注">
                  <el-input v-model="ruleForm.beizhu"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <div class="add-main-list">
        <div class="list-button-group">
          <div class="button-item">
            <!-- @click="insertEvent()" -->
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="DialogOpenWuzi([])"
            >
              新增
            </el-button>
          </div>
          <div class="button-item">
            <el-button type="danger" icon="el-icon-delete" @click="delwzDetail">
              删除
            </el-button>
          </div>
          <!-- <div class="button-item">
            <el-button type="primary" icon="el-icon-s-check" @click="importShow = true">
              导入入库单明细
            </el-button>
          </div>
          <div class="button-item">
            <el-button type="primary" icon="el-icon-s-check">
              下载模板
            </el-button>
          </div> -->
        </div>
        <div class="table-list" id="table-list">
          <ux-grid
            border
            show-overflow
            ref="plxTable"
            keep-source
            :height="tabheight"
            :highlightCurrentRow="false"
            widthResize
            :rules="ruleslist"
            @selection-change="selectChange"
            :edit-config="{ trigger: 'click', mode: 'row' }"
          >
            <ux-table-column type="checkbox" width="50" />
            <ux-table-column
              type="index"
              title="序号"
              width="50"
            ></ux-table-column>
            <ux-table-column
              field="zhengshibmm"
              title="编目码"
            ></ux-table-column>
            <ux-table-column field="wuzimc" title="物资名称"></ux-table-column>
            <ux-table-column field="jiliangdw" title="单位"></ux-table-column>
            <ux-table-column
              field="zhiliangdj"
              title="质量等级"
            ></ux-table-column>
            <ux-table-column
              field="huoweimc"
              title="存放货位"
            ></ux-table-column>
            <ux-table-column
              field="baoyangzq"
              title="保养周期（月）"
            ></ux-table-column>
            <ux-table-column
              field="weihubyrq"
              title="本次保养时间"
              edit-render
              width="200"
            >
              <template v-slot:edit="{ row }">
                <el-date-picker
                  v-model="row.weihubyrq"
                  type="datetime"
                  placeholder="选择日期时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                ></el-date-picker>
              </template>
            </ux-table-column>
            <ux-table-column
              field="xiacibyrq"
              title="下次保养时间"
              edit-render
              width="200"
            >
              <template v-slot:edit="{ row }">
                <el-date-picker
                  v-model="row.xiacibyrq"
                  type="datetime"
                  placeholder="选择日期时间"
                  value-format="yyyy-MM-dd"
                ></el-date-picker>
              </template>
            </ux-table-column>
          </ux-grid>
          <div class="submit-button">
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submitForm('ruleForm')"
            >
              确认
            </el-button>
            <el-button type="info" icon="el-icon-delete" @click="goBack">
              取消
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!--新增 物资 -->
    <page-1-add-wuzi
      ref="wuzi-dialog"
      @fetch-data="fetchData"
    ></page-1-add-wuzi>
    <!-- 导入文件start -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="importShow"
      destroy-on-close
      title="导入文件"
      class="m-dialog"
      :append-to-body="true"
    >
      <upload api-url="xqzdDetail/import" @success="uploadSuccess" />
    </el-dialog>
  </div>
</template>
<script>
  import Upload from '@/components/Upload/upload'
  import { queryKufangList } from '@/api/warehousing'
  import page1AddWuzi from './page1AddWuzi.vue'
  // import { saveWeihubyd } from '@/api/kunei'

  export default {
    name: 'Page1Add',
    components: { Upload, page1AddWuzi },
    props: {
      gobacktitle: {
        type: String,
        default: '新增',
        required: true,
      },
      datarow: {
        type: Object,
        default: function () {
          return {}
        },
      },
      zhuangtaiArr: {
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        show: true,
        cangkuid: '',
        insertData: [],
        ruleForm: {
          weihubydmx: [],
        },
        rules: {
          wuziqf: [
            { required: true, message: '请选择物资区分', trigger: 'blur' },
          ],
          kufangid: [
            { required: true, message: '请选择库房', trigger: 'blur' },
          ],
          wuzizyid: [
            { required: true, message: '请选择物资分类', trigger: 'blur' },
          ],
          zhuguandwid: [
            { required: true, message: '请选择主管单位', trigger: 'blur' },
          ],
          zhuangtai: [
            { required: true, message: '请选择状态', trigger: 'blur' },
          ],
        },
        ruleslist: {
          kucunhzsl: [
            { required: true, message: '请输入数量', trigger: 'blur' },
          ],
        },
        tabheight: 500,
        importShow: false,
        KufangList: [],

        Wzzylist: [],
        Zgdwlist: [],
      }
    },
    created() {},
    mounted() {
      this.cangkuid = this.$store.getters['user/cangkuid']
      this.getKufangList()
      let height = document.getElementById('table-list').offsetHeight
      this.tabheight = height - 94
      let ruleForm2 = Object.assign({}, this.datarow)
      this.ruleForm = Object.assign({}, ruleForm2.row)
      this.ruleForm.cangkuid = this.cangkuid
      this.ruleForm.weihubydmx = ruleForm2.weihubydmx
      this.ruleForm.zhuangtai = this.ruleForm.zhuangtai == '待保养' ? '10' : '5'
      this.$nextTick(() => {
        this.$refs.plxTable.reloadData(this.ruleForm.weihubydmx)
      })
    },
    methods: {
      goBack() {
        this.$parent.addshow = false
      },

      DialogOpenWuzi(row) {
        this.$refs['wuzi-dialog'].showEdit(row)
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.ruleForm.weihubydmx =
              this.$refs.plxTable.getTableData().fullData
            const mxLen = this.$refs.plxTable.getTableData().fullData
            if (mxLen.length == 0) {
              this.$message({
                message: '请添加明细',
                type: 'warning',
              })
              return
            }
            this.$refs.plxTable.validate(true).then(async (data) => {
              if (data) {
                // 有错误
              } else {
                // console.log(this.ruleForm)
                let ruleForm2 = Object.assign({}, this.ruleForm)
                delete ruleForm2.create_time
                delete ruleForm2.modify_time
                delete ruleForm2.is_delete

                // const msg = await saveWeihubyd(ruleForm2)
                this.$message({
                  message: msg.msg,
                  type: 'success',
                })
                if (this.gobacktitle == '新增') {
                  this.resetForm() //重置表单
                }
                this.$parent.resetlist()
              }
            })
          } else {
            return false
          }
        })
      },

      resetForm(formName) {
        this.ruleForm = Object.assign({ weihubydmx: [] }, [])
        this.$nextTick(() => {
          this.$refs.plxTable.reloadData([])
        })
        // this.$refs[formName].resetFields()
      },
      selectChange(e) {},
      // 添加插入行
      async insertEvent(row) {
        let record = this.insertData
        // 加入行, 表示加入行的row对象，row为空则插入第一行，为-1插入最后一行
        let { row: newRow } = await this.$refs.plxTable.insertRow(record)
        // 激活单元格编辑
        await this.$refs.plxTable.setActiveRow(newRow)
      },
      delwzDetail() {
        this.$refs.plxTable.remove(this.$refs.plxTable.getCheckboxRecords())
      },
      uploadSuccess() {
        this.importShow = false
      },
      oninput(num) {
        return num.replace(/\D/g, '')
      },
      async getKufangList() {
        //库房
        const kfData = await queryKufangList({ cangkuid: this.cangkuid })
        this.KufangList = kfData.data

        this.Zgdwlist = await this.$util.getZgdw()
        //物资分类
        this.Wzzylist = await this.$util.getWzzy()
      },

      fetchData(data) {
        this.insertData = []
        data.forEach((item) => {
          this.insertData.push({
            wuziid: item.id,
            wuzimc: item.wuzimc,
            wuzibm: item.wuzibm,
            zhengshibmm: item.zhengshibmm,
            guigexh: item.guigexh,
            jiliangdw: item.jiliangdw,
            huoweimc: item.huoweimc,
            baoyangzq: item.baoyangzq,
            zhiliangdjmc: item.zhiliangdjmc,
            weihubyrq: item.weihubyrq,
          })
        })
        this.insertEvent()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .add-container {
    position: fixed;
    left: 0;
    top: 0.75rem;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background-color: #f6f8f9;
    padding: 0 0.2rem 1rem 0.2rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .add-top {
      padding: 0.1rem 0;

      ::v-deep .el-page-header__title {
        font-size: 0.16rem !important;
      }
    }

    .add-main {
      flex: 1;
      font-weight: bold;
      display: flex;
      flex-direction: column;
      overflow: auto;

      .add-main-top {
        padding: 0.2rem;
        background: #fff;
        border-radius: 0.05rem;

        .add-main-tital {
          font-size: 0.18rem;
          border-left: 0.1rem solid rgb(121, 189, 255);
          padding-left: 0.1rem;

          p {
            margin: 0;
          }
        }

        .search-gropu-item {
          margin-top: 0.2rem;
          width: 90%;
          display: flex;
          // label {
          //   white-space: nowrap;
          //   line-height: 32px;
          //   margin-right: 10px;
          // }
        }

        .el-select {
          width: 100%;
        }
      }

      .add-main-list {
        flex: 1;
        margin-top: 20px;
        background: #fff;
        padding: 20px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;

        .list-button-group {
          display: flex;
          flex-direction: row;
          padding-bottom: 20px;

          .button-item {
            padding: 0px 10px;
          }

          .button-item:first-child,
          &:last-child {
            padding: 0px 10px 0 0;
          }
        }

        .table-list {
          flex: 1;
        }
      }

      .el-button {
        font-size: 0.14rem !important;
      }

      ::v-deep {
        .elx-table--header-wrapper {
          background-color: rgb(233, 243, 255);
        }

        .elx-table {
          font-size: 0.14rem !important;
        }

        .elx-body--column {
          height: 0.48rem !important;
          line-height: 0.24rem;
        }

        .elx-header--column {
          background-image: none !important;
          height: 0.48rem !important;
        }

        .el-date-editor.el-input,
        .el-date-editor.el-input--inner {
          width: 100% !important;
        }

        .el-form-item--small.el-form-item {
          margin-bottom: 0px;
          margin-top: 18px;
        }
      }

      .submit-button {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>
